﻿
package com.ostrich.manpower.service.impl;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Param;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.base.service.BaseService;
import com.ostrich.manpower.bean.SendMsg;
import com.ostrich.manpower.bean.TostHrBaseinfo;
import com.ostrich.manpower.bean.TostHrCheckinlog;
import com.ostrich.manpower.bean.TostHrFactpayinfo;
import com.ostrich.manpower.bean.TostHrMealaddinfo;
import com.ostrich.manpower.bean.TostHrPostinfo;
import com.ostrich.manpower.bean.TostHrSalaryinfo;
import com.ostrich.manpower.bean.TostHrSubsalaryinfo;
import com.ostrich.manpower.bean.TostHrSubsidyinfo;
import com.ostrich.manpower.common.FormulaUtil;
import com.ostrich.manpower.common.StringUtils;
import com.ostrich.manpower.service.TostHrSubsalaryinfoDao;
import com.ostrich.project.bean.TostPjNoncashinfo;
import com.ostrich.system.analysis.ParaminfoUtil;
import com.ostrich.system.bean.TostSysParaminfo;
/**
 * @author wangyinan
 * @brief    模块功能简单描述
 * @details  模块功能详细描述
 */
@Service("tostHrSubsalaryinfoService")
public class TostHrSubsalaryinfoService<T> extends BaseService<T> {
	private final static Logger log= Logger.getLogger(TostHrSubsalaryinfoService.class);

	@Autowired
    private TostHrSubsalaryinfoDao<T> dao;
	@Autowired(required=false)
	private TostHrSalaryinfoService<TostHrSalaryinfo> tosthrsalaryinfoservice;
	@Autowired(required=false)
	private TostHrMealaddinfoService<TostHrMealaddinfo> tosthrmealaddinfoservice;
	@Autowired(required=false)
	private TostHrCheckinlogService<TostHrCheckinlog> tosthrcheckinlogservice;
	@Autowired(required=false)
	private TostHrSubsidyinfoService<TostHrSubsidyinfo> tosthrsubsidyinfoservice;
	@Autowired(required=false)
	private TostHrPostinfoService<TostHrPostinfo> tosthrpostinfoservice;
	
	
	public TostHrSubsalaryinfoDao<T> getDao() {
		return dao;
	}
	
	public List<T> queryByHrid(Integer hrid) {
		return getDao().queryByHrid(hrid);
	}
	public List<T> queryByHrMonthHrid(@Param("hrMonth")String hrMonth,@Param("hrId")int hrId){
		return dao.queryByHrMonthHrid(hrMonth, hrId);
	}
	public void deleteByhrMonth(String hrMonth,int hrId){
		dao.deleteByhrMonth(hrMonth,hrId);
	}
	
	public TostHrSubsalaryinfo addSubsidyinfo(String hmeMonth,TostHrBaseinfo bsinfo) throws Exception{
		TostHrSubsalaryinfo tsli=new TostHrSubsalaryinfo();
		
		tsli.setHrId(bsinfo.getId());
		tsli.setHsuMonth(hmeMonth);
		
		List<TostHrSubsalaryinfo> lst=(List<TostHrSubsalaryinfo>) dao.queryByHrMonthHrid(hmeMonth, bsinfo.getId());
		
		//误餐补贴(餐补表内容)
		List<TostHrMealaddinfo> ls= tosthrmealaddinfoservice.queryIdHridHme(hmeMonth, bsinfo.getId());
		if(ls!=null&& ls.size()>0){
			TostHrMealaddinfo  hm = ls.get(0);
			tsli.setHsuMealsubsidies(hm.getHmeMealsubsidies());
		}else
			tsli.setHsuMealsubsidies(new Float(0));
		
		TostHrSubsidyinfo sub=tosthrsubsidyinfoservice.queryByHrid(bsinfo.getId());
		//定义专家补贴（只有补贴表才有这个字段，工资补贴表是没这个字段的）,知识管理员补贴（只有补贴表才有这个字段，工资补贴表是没这个字段的）
		Float jzbt,jsgly;
		if(sub!=null){
			
			//异地补贴
			tsli.setHsuAnplace(sub.getHsbRemote());
			//电脑补贴
			tsli.setHsuComputersub(sub.getHsbComputer());
			//其他补贴
			tsli.setHsuOthersub(sub.getHsbOthersubs());
			//学历/职称补贴
			tsli.setHsuDegreetitlesub(sub.getHsbEducation());
			
			//设置交通补贴信息
			tsli.setHsuTrafficsubsidy(sub.getHsbTraffic());
			
			//通讯补贴
			tsli.setHsuCommunisub(sub.getHsbCommunication());
			//知识管理员补贴设值
			jsgly = sub.getHsbKnowadmin()!=null?sub.getHsbKnowadmin():new Float(0);
			//专家补贴设值
			jzbt = sub.getHsbExpert()!=null?sub.getHsbExpert():new Float(0);
		}else{
			tsli.setHsuAnplace(new Float(0));
			tsli.setHsuComputersub(new Float(0));
			tsli.setHsuOthersub(new Float(0));
			tsli.setHsuDegreetitlesub(new Float(0));
			tsli.setHsuTrafficsubsidy(new Float(0));
			tsli.setHsuCommunisub(new Float(0));
			//专家补贴设值为0。
			jzbt = new Float(0);
			//知识管理员补贴设值为0
			jsgly = new Float(0);
		}
		//补贴合计 = 误餐补贴+异地补贴+电脑补贴+其他补贴+学历补贴+交通补贴+通讯补贴+专家补贴+知识管理员补贴
		Float mealsubsidies = tsli.getHsuMealsubsidies()!=null?tsli.getHsuMealsubsidies():new Float(0);
		Float anplace = tsli.getHsuAnplace()!=null?tsli.getHsuAnplace():new Float(0);
		Float computersub = tsli.getHsuComputersub()!=null?tsli.getHsuComputersub():new Float(0);
		Float othersub = tsli.getHsuOthersub()!=null?tsli.getHsuOthersub():new Float(0);
		Float degree = tsli.getHsuDegreetitlesub()!=null?tsli.getHsuDegreetitlesub():new Float(0);
		Float trafficsub = tsli.getHsuTrafficsubsidy()!=null?tsli.getHsuTrafficsubsidy():new Float(0);
		Float communisub = tsli.getHsuCommunisub()!=null?tsli.getHsuCommunisub():new Float(0);
		
		tsli.setHsuAccount(mealsubsidies+anplace+computersub+othersub+degree+trafficsub+communisub+jzbt+jsgly);
		if(lst!=null && lst.size()>0){
			tsli.setId(lst.get(0).getId());
			dao.update((T) tsli);
		}else
			dao.add((T) tsli);
		return tsli;
		}
	
	//没用？
	public TostHrSubsalaryinfo addSubsalaryinfo(String hmeMonth,int hrId) throws Exception{
		
		TostHrSubsalaryinfo tsli=new TostHrSubsalaryinfo();//补贴表实体类
		tsli.setHrId(hrId);
		//tsli.setHsuMonth(Integer.toString(hmeMonth));
		
		if(!tosthrsalaryinfoservice.isHrMealadByMouth(hmeMonth, hrId)){
			//餐补表为空
		}else if(!tosthrsalaryinfoservice.isSubsidyinfo(hrId)){
			System.out.println("Subsidyinfo表为空！");
		}else if(!tosthrsalaryinfoservice.isCheckInlog(hrId)){
			System.out.println("Check表为空");
		}else{
			float sum=0;//计算小计用的
			
			List<TostHrMealaddinfo> meallist=tosthrmealaddinfoservice.queryIdHridHme(hmeMonth, hrId);
			for(TostHrMealaddinfo thmi:meallist){
				float mealsubsides=thmi.getHmeMealsubsidies();
				sum+=mealsubsides;
				tsli.setHsuMealsubsidies(mealsubsides);//餐补贴
			}
			List<TostHrCheckinlog> checklist=tosthrcheckinlogservice.queryCheckInLogByHrId(hrId);
			for(TostHrCheckinlog tci:checklist){
				float anplaec=tci.getHciSubsidyTotal();
				sum+=anplaec;
				tsli.setHsuAnplace(anplaec);//考勤异地补贴
			}
			List<TostHrSubsidyinfo> subsidyinfolist=tosthrsubsidyinfoservice.querySubsidyinfoByHrId(hrId);
			for(TostHrSubsidyinfo thsi :subsidyinfolist){
				float computer=thsi.getHsbComputer();
				sum+=computer;
				tsli.setHsuComputersub(computer);//电脑补贴
				float education=thsi.getHsbEducation();
				sum+=education;
				tsli.setHsuDegreetitlesub(education);//学历职称补贴
				//float othersuns=thsi.getHsbOthersubs()+thsi.getHsbSkillsubs();
				//sum+=othersuns;// go-todo  wyn
				//tsli.setHsuOthersub(othersuns);//其他补贴
			}
			
			tsli.setHsuTrafficsubsidy(null);//？？？？？交通补贴。
			
			tsli.setHsuAccount(sum);//补贴合计
			this.add((T) tsli);
		}
		return tsli;
	}
	
	
	/****
	 * 通过岗位获取交通费用
	 * @param postid,parmid
	 * @param parmid  113 表示交通补贴，114表示通讯补贴
	 * @return
	 */
	public Float getSubsidyPay(String postid,Integer parmid) {
		Map<Integer, TostSysParaminfo> levemap = ParaminfoUtil
				.getParamByPid(20);
		TostHrPostinfo pt = tosthrpostinfoservice.queryById(postid);
		//判断岗位不存在的时候返回0
		if (pt == null)
			return new Float(0);
		else {
			Integer ptype = (StringUtils.isNum(pt.getHpoType()))?Integer.parseInt(pt.getHpoType()):null;
			//判断岗位类型是否存在
			if(ptype!=null && levemap.get(ptype)!=null){
				// 查询交通补贴固定 113 114 表示通讯
				Map<String, Object> map = FormulaUtil.formulaById(12,levemap.get(ptype), parmid);
				Float cash = (map != null) ? (Float) map.get("cash") : new Float(0);
				return cash != null ? cash : new Float(0);
			}else
				return new Float(0);
		}
	}
}
